<?php
	require('authController.class.php');
	class indexController extends authController{
		
		//首页
		function index(){
			custom('Controller', 'carconfig') -> carconf();	
			$carinfoobj = M('carinfo');
			$data = custom('Controller', 'pages') -> page($carinfoobj);
			VIEW::assign(array('data' => $data));
			VIEW::display('index/index.html');
		}
		
		//car详情页
		function carinfo(){
			$data = M('carinfo') -> getcarinfo(intval($_GET['carid']));
			$carconfobj = custom('Controller', 'carconfig');
			$carconfobj -> carconf();
			$carconfobj -> carpic($data[car_pic]);
			VIEW::assign(array('data' => $data));
			VIEW::display('index/carinfo.html');
			
		}
		
		//修改审核状态
		public function carstatus(){
			M('carinfo') -> setcarstatus(intval($_GET['status']));
			echo 'index.php?c=index&m=carinfo&carid='.intval($_GET['carid']);
		}
		
		//删除车辆信息
		public function delcar(){
			$carinfoobj = M('carinfo');
			$data = $carinfoobj -> del_by_id(intval($_GET['carid']));
			$this -> showmessage('操作成功！','index.php');
		}
		
		//添加，修改车辆信息
		public function addcar(){
			//判断是否有post数据
			if(empty($_POST)){//没有post数据，就显示添加，修改界面
				//读取旧数据,车辆信息carid $_GET,有就修改，没就添加
				if(isset($_GET['carid'])){
					$data = M('carinfo') -> getcarinfo(intval($_GET['carid']));
				}else{
					$data = array();
				}
				$carconfobj = custom('Controller', 'carconfig');
				$carconfobj -> carconf();
				$carconfobj -> carlabel();
				$data = custom('Controller', 'pages') -> page(M('carinfo'));
				VIEW::assign(array('data' => $data));
				VIEW::display('index/addcar.html');
			}else{//进入添加，修改程序
				$this -> carinfosubmit();
				
			}
		}
		
		//判断$result的值，来做相应的提示
		private function carinfosubmit(){
			$carinfoobj = M('carinfo');
			$result = $carinfoobj -> carsubmit($_POST);
			if($result == 0){
				$this -> showmessage('操作失败！','index.php?c=index&m=addcar');
			}
			if($result == 1){
				$this -> showmessage('添加成功','index.php?c=index&m=addattach');
			}
			if($result == 2){
				$this -> showmessage('修改成功','index.php?c=index&m=addattach&carid='.$_POST['carid']);
			}
		}

		//图片上传
		public function addattach(){
			//判断是否有post数据
			if(empty($_POST)){//没有post数据，就显示添加，修改界面
				//读取旧数据,传递新闻id $_GET,有就修改，没就添加
				if(isset($_GET['carid'])){
					if($_GET['carid'] == 'x'){
						$carinfoobj = M('carpic');
						$result = $carinfoobj -> carpicsubmit();
						$this -> showmessage('设置成功','index.php?c=index&m=carinfo&carid='.$result);
					}else{
						$data = M('carinfo') -> getcarinfo(intval($_GET['carid']));
						custom('Controller', 'carconfig') -> carpic($data[car_pic]);
					}
				}else{
					$data = array();
				}
				VIEW::assign(array('data' => $data));
				VIEW::display('index/addattach.html');
			}else{//进入添加，修改程序
				$carinfoobj = M('carpic');
				$result = $carinfoobj -> carpicsubmit();
				$this -> showmessage('设置成功','index.php?c=index&m=carinfo&carid='.$result);
			}
		}
		
		//图片删除
		public function cardelpic(){
			$carinfoobj = M('carpic');
			$result = $carinfoobj -> del_by_id($_GET['carpicid'],intval($_GET['carid']));
			$this -> showmessage('删除成功','index.php?c=index&m=carinfo&carid='.intval($_GET['carid']));
		}
		
		//信息提示
		public function showmessage($cont, $URL){
			echo "<script> console.log('".$cont."');parent.location.href='".$URL."'; </script>";  
		}
		
	}
?>